﻿using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Windows.Forms;

namespace 财务管理
{
    public partial class Ygxx : Form
    {
        private int row_index = 0;

        private bool tj = false;

        private bool excel = false;

        public Ygxx()
        {
            InitializeComponent();
            this.tsb_tj.Enabled = false;
            this.tsb_qx.Enabled = false;
            this.Readonly();
            string constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
            SqlConnection coon = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand
            {
                CommandText = "select * from ygxx",
                Connection = coon
            };
            SqlDataAdapter dbAdapter = new SqlDataAdapter(cmd);
            coon.Open();
            DataSet ds = new DataSet();
            dbAdapter.Fill(ds);
            coon.Close();
        }

        private void Readonly()
        {
            for (int i = 0; i < this.dgv_ygxx.Rows.Count; i++)
            {
                this.dgv_ygxx.Rows[i].ReadOnly = true;
            }
        }

        private void tsb(bool state)
        {
            bool other = (byte)((!state) ? 1 : 0) != 0;
            this.tsb_tj.Enabled = state;
            this.tsb_xg.Enabled = state;
            this.tsb_sc.Enabled = state;
            this.tsb_excel.Enabled = state;
            this.tsb_bc.Enabled = other;
            this.tsb_qx.Enabled = other;
        }

        private void tsb_tj_Click(object sender, EventArgs e)
        {
            this.tsb(false);
            this.tj = true;
            this.dgv_ygxx.Rows.Add();
            this.row_index = this.dgv_ygxx.Rows.Count - 1;
            this.dgv_ygxx.Rows[this.row_index].ReadOnly = false;
            this.dgv_ygxx.CurrentCell = this.dgv_ygxx.Rows[this.row_index].Cells[0];
            string constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
            SqlConnection coon = new SqlConnection(constr);
            SqlCommand cmd = coon.CreateCommand();
            cmd.CommandText = "select max(yid) from ygxx";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            coon.Open();
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count >= 1)
            {
                this.dgv_ygxx["yid", this.row_index].Value = Convert.ToInt32(ds.Tables[0].Rows[0][0]) + 1;
            }
            coon.Close();
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "C:\\Users\\Administrator\\Desktop\\text";
            sfd.FileName = "员工信息表";
            sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            sfd.Filter = "Excel文件(*.xls)|*.xls";
            sfd.ValidateNames = true;
            sfd.CheckPathExists = true;
            string strName = string.Empty;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                strName = sfd.FileName;
            }
            if (!(strName == ""))
            {
                this.ExportDataGridview(this.dgv_ygxx, true, strName);
            }
        }

        private bool ExportDataGridview(DataGridView datagridview, bool isShowExcle, string path)
        {
            if (datagridview.Rows.Count == 0)
            {
                return false;
            }
            Microsoft.Office.Interop.Excel.Application excel = new ApplicationClass();
            Workbook myWorkBook2 = excel.Application.Workbooks.Add(true);
            excel.Visible = isShowExcle;
            for (int k = 0; k < this.dgv_ygxx.Columns.Count; k++)
            {
                excel.Cells[1, k + 1] = this.dgv_ygxx.Columns[k].HeaderText;
            }
            for (int k = 0; k < datagridview.Rows.Count; k++)
            {
                for (int i = 0; i < datagridview.Columns.Count; i++)
                {
                    excel.Cells[k + 2, i + 1] = datagridview.Rows[k].Cells[i].Value;
                }
            }
            object missing = Missing.Value;
            excel.ActiveWorkbook.Saved = true;
            excel.ActiveWorkbook.SaveAs(path, XlFileFormat.xlExcel9795, missing, missing, false, false, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
            myWorkBook2.Close(missing, missing, missing);
            myWorkBook2 = null;
            excel.Quit();
            return true;
        }

        private void toolStripButton7_Click(object sender, EventArgs e)//退出
        {
            base.Close();
        }

    }
}
